﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1.Filtry
{
    class WhiteRohrer
    {
         public Bitmap bitmapFiltr { get; set; }
        private int MacierzWidthIHight,width,height;
        private Bitmapp bitmap; double K;

        public WhiteRohrer(int MacierzWidthIHight, int width, int height, Bitmapp bitmap, double K)
        {
            this.width = width; this.height = height;
            this.bitmap = bitmap; this.MacierzWidthIHight = MacierzWidthIHight;
            this.K = K;
        }

        internal System.Drawing.Bitmap ZwrocObrazekWhiteRohrer()
        {
            this.bitmapFiltr = new Bitmap(this.width, this.height);
            Double SumaR = 0, SumaG = 0, SumaB = 0;
            double Prog = 0;
            int xPixel , yPixel;

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (j <= MacierzWidthIHight / 2 || j > this.width - MacierzWidthIHight / 2 - 1 || i <= MacierzWidthIHight / 2 || i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                    }
                    else
                    {
                        SumaR = 0; SumaG = 0; SumaB = 0;
                        xPixel = j - MacierzWidthIHight / 2; yPixel = i - MacierzWidthIHight / 2;
                        Prog = 0;

                        for (int ii = 0; ii < MacierzWidthIHight; ii++)
                        {
                            for (int jj = 0; jj < MacierzWidthIHight; jj++)
                            {
                                SumaR = SumaR + Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel));
                                SumaB = SumaB + Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel));
                                SumaG = SumaG + Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel));
                                xPixel++;
                            }
                            yPixel++; xPixel = j - MacierzWidthIHight / 2;

                        }
                        SumaB = SumaB / (MacierzWidthIHight * MacierzWidthIHight);
                        SumaG = SumaG / (MacierzWidthIHight * MacierzWidthIHight);
                        SumaR = SumaR / (MacierzWidthIHight * MacierzWidthIHight);

                        Prog = ((SumaR + SumaG + SumaB) / 3) / this.K;

                        if (bitmap.GetPixelR(j, i) < Prog || bitmap.GetPixelG(j, i) < Prog || bitmap.GetPixelB(j, i) < Prog)
                            bitmapFiltr.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                        else bitmapFiltr.SetPixel(j, i, Color.FromArgb(255, 255, 255));

                    }
                }
            }

            return this.bitmapFiltr;
        }
    }
}
